﻿<%@ WebHandler Language="C#" Class="service_dataloadservice" %>

using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using WuDada.Trade.DataLoader.Service;
using WuDada.Core.SystemApplications;
using WuDada.Trade.DataLoader.Domain;
using WuDada.Trade.DataLoader.Utility;
using Common.Logging;

public class service_dataloadservice : BaseAjaxService, IHttpHandler
{
    #region Fields
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private TradeDataLoaderFactory m_TradeDataLoaderFactory = new TradeDataLoaderFactory();
    private IDataLoadService m_DataLoadService;
    private bool m_IsRegisted = false;
    #endregion

    #region PageEvent
    public override Page RegisterMethods()
    {
        if (m_DataLoadService == null)
        {
            m_DataLoadService = m_TradeDataLoaderFactory.GetDataLoadService();
        }
        if (!m_IsRegisted)
        {
            #region 註冊Method
            InsertMethod(this, "createstockdata", "CreateStockData");
            #endregion

            m_IsRegisted = true;
        }
        return this;
    }
    #endregion

    #region Methods 實做

    public void CreateStockData()
    {
        try
        {
            string stockCode = QueryStringHelper.GetString("stockCode", string.Empty);
            string period = QueryStringHelper.GetString("period", string.Empty);
            string strDataTime = QueryStringHelper.GetString("dataTime", string.Empty);
            double openValue = QueryStringHelper.GetDouble("openValue", 0.00);
            double highValue = QueryStringHelper.GetDouble("highValue", 0.00);
            double lowValue = QueryStringHelper.GetDouble("lowValue", 0.00);
            double closeValue = QueryStringHelper.GetDouble("closeValue", 0.00);
            string strVolume = QueryStringHelper.GetString("volumeValue", "0");
            int volume = int.Parse(strVolume.Replace(",", ""));
            DateTime dataTime = GoogleStockDataConvertUtil.ToDateTime(strDataTime);

            //先判斷該筆資料是否有建立過
            if (m_DataLoadService.GetStockData(stockCode, dataTime) != null)
            {
                AjaxHelper.WriteAjaxResult(true, null, "exist");
            }
            else
            {
                StockDataVO stockDataVO = new StockDataVO();
                stockDataVO.StockCode = stockCode;
                stockDataVO.Period = (StockDataVO.StockDataPeriod)Enum.Parse(typeof(StockDataVO.StockDataPeriod), period);
                stockDataVO.DataTime = dataTime;
                stockDataVO.OpenValue = openValue;
                stockDataVO.HighValue = highValue;
                stockDataVO.LowValue = lowValue;
                stockDataVO.CloseValue = closeValue;
                stockDataVO.Volume = volume;

                m_DataLoadService.CreateStockData(stockDataVO);
                
                AjaxHelper.WriteAjaxResult(true, null, "ok");             
            }

            
        }
        catch (Exception ex)
        {
            AjaxHelper.WriteAjaxError(ex.Message, ex);
        }
    }

    #endregion
}